const app = getApp()
// pages/myTask/myTask.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:'',
    taskType:'2',
    userType:'',
    pageNum:1,
    rewardList:[],
    bindingList:[],
    rewardStatus:'',
    bindStatus:'',
    rewardLoad:false,
    bindingLoad:false
  },
  // 任务类型
  taskTypeBtn:function(e){
    var {index} = e.currentTarget.dataset
    this.setData({
      taskType: index
    })
    this.taskNum();
  },


  rewardBtn(e){
    let { taskstatus } = e.currentTarget.dataset
    this.setData({
      rewardStatus:taskstatus,
      rewardList:[],
    })
    this.taskList(1)
  },

  bindBtn(e){
    let { taskstatus } = e.currentTarget.dataset
    this.setData({
      bindStatus:taskstatus,
      bindingList:[],
    })
    this.taskList(2)
  },



  // 任务列表
  taskList:function(taskType){
    var { pageNum, rewardStatus,bindStatus,rewardList,bindingList, userType } = this.data
    var _this = this
    var url = ''
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      rewardLoad:false,
      bindingLoad:false,
    })
    if (userType == 1){//1是雇主模式2是威客模式
      url = app.globalData.baseUrl + 'myTask/index'
    }else{
      url = app.globalData.baseUrl + 'myTask/myAccept'
    }

    let taskstatus

    if(taskType===1){
      taskstatus=rewardStatus
    }else {
      taskstatus=bindStatus
    }

    wx.request({
      url: url,
      data: {
        type: taskType,
        status: taskstatus,
        page: pageNum,
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        _this.setData({
          rewardLoad:false,
          bindingLoad:false,
        })
        console.log(res)
        if (res.data.code == 1000) {
          if(taskType===1){
            _this.setData({
              rewardList: [...rewardList, ...res.data.data.data]
            })
          }else {
            _this.setData({
              bindingList: [...bindingList, ...res.data.data.data]
            })
          }
          _this.setData({
            rewardLoad:_this.data.rewardList.length===0,
            bindingLoad:_this.data.bindingList.length===0,
          })

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 点击任务列表
  shopList: function (e) {
    var { id } = e.currentTarget.dataset;
    console.log(id);
    wx.navigateTo({
      url: '/taskPage/pages/taskDetail/taskDetail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userType: wx.getStorageSync('userType')//1是雇主模式2是威客模式
    })

    // this.taskNum();

  },
  // 任务数量
  taskNum:function(){
    var { taskType, userType } = this.data
    var _this = this
    var url = '';
    wx.showLoading({
      title: '加载中',
    })
    if (userType == 1){//1是雇主模式2是威客模式
      url = app.globalData.baseUrl + 'myTask/indexCount'
    }else{
      url = app.globalData.baseUrl + 'myTask/myAcceptCount'
    }
    wx.request({
      url: url,
      data: {
        type: taskType,
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            taskNum: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      rewardList:[],
      bindingList:[],
    })
    this.taskList(1)
    this.taskList(2)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var { pageNum,taskType } = this.data
    pageNum ++;
    this.setData({
      pageNum
    })
    if(taskType==='1'){
      this.taskList(1);
    }else {
      this.taskList(2);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
